package com.forum.article.controller;


import com.forum.article.entity.ForumArticleTag;
import com.forum.article.entity.ForumArticleType;
import com.forum.article.mapper.ForumArticleTypeMapper;
import com.forum.article.service.ForumArticleTypeService;
import com.forum.article.vo.Result;
import com.forum.common.constant.ErrorCodeEnum;
import com.forum.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bncgnc
 * @since 2022-01-04
 */
@RestController
@RequestMapping("/article/forumArticleType")
public class ForumArticleTypeController {
    @Autowired
    ForumArticleTypeService forumArticleTypeService;
    @Autowired
    ForumArticleTypeMapper forumArticleTypeMapper;

    /**
     * 新增文章类型
     * @param forumArticleType
     * @return
     */
    @PostMapping("/addType")
    public R addType(@RequestBody ForumArticleType forumArticleType) {
        if ( forumArticleType != null ) {
            forumArticleTypeMapper.insert(forumArticleType);
            return R.ok();
        } else {
            return R.error(ErrorCodeEnum.VALID_EXCEPTION.getCode() , ErrorCodeEnum.VALID_EXCEPTION.getMessage());
        }
    }

    /**
     * 修改文章类型
     * @param forumArticleType
     * @return
     */
    @PostMapping("/updateType")
    public R updateType(@RequestBody ForumArticleType forumArticleType) {
        if ( forumArticleType != null ) {
            forumArticleTypeMapper.updateById(forumArticleType);
            return R.ok();
        } else {
            return R.error(ErrorCodeEnum.VALID_EXCEPTION.getCode() , ErrorCodeEnum.VALID_EXCEPTION.getMessage());
        }
    }


    /**
     * 删除文章类型
     * @param id
     * @return
     */
    @PostMapping("/deleteType")
    public R deleteType(@RequestBody Long id) {
        if ( id != null ) {
            forumArticleTypeMapper.deleteById(id);
            return R.ok();
        } else {
            return R.error(ErrorCodeEnum.VALID_EXCEPTION.getCode() , ErrorCodeEnum.VALID_EXCEPTION.getMessage());
        }
    }


    @GetMapping("/allType")
    public R Types(){
        return forumArticleTypeService.findAll();
    }

    @GetMapping("detail")
    public Result typeDetail(){
        return forumArticleTypeService.findAllDetail();
    }


//分类导航
    @GetMapping("detail/{id}")
    public Result typeDetailById(@PathVariable("id") Long id){
        return forumArticleTypeService.typeDetailById(id);
    }
}

